package com.zhouyu.controller;

import com.zhouyu.az_service.ExcelService;
import com.zhouyu.service.PageExcelService;
import com.zhouyu.service.PoolExcelService;
import com.zhouyu.service.PoolZipService;
import com.zhouyu.service.PoolZipTwoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author lipengze
 * @version 1.0
 * @description: TODO
 * @date 2024/11/28 11:20
 */
@Slf4j
@RestController
@RequestMapping("excel")
public class ExcelController {
    @Resource
    private ExcelService excelService;
    @Resource
    private PageExcelService pageexcelService;
    @Resource
    private PoolExcelService poolexcelService;

    @Resource
    private PoolZipService poolexzipService;
    @Resource
    private PoolZipTwoService poolexzipTwoService;

    @GetMapping("export")
    public void export() {
        log.info("export excel");
        excelService.export();
    }
    @GetMapping("pageExport")
    public void pageExport() {
        log.info("pageExport excel");
        pageexcelService.export();
    }
    @GetMapping("poolExport")
    public void poolExport() {
        log.info("poolExport excel");
        poolexcelService.export();
    }
//    @GetMapping("/download")
//    public ResponseEntity<byte[]> download() throws IOException {
//        try (FileInputStream fis = poolexzipService.exportToZip()) {// 调用服务方法生成ZIP文件
//            byte[] bytes = new byte[fis.available()];// 创建字节数组
//            fis.read(bytes);// 读取ZIP文件内容到字节数组
//
//            HttpHeaders headers = new HttpHeaders();// 创建HTTP响应头
//            headers.add("Content-Disposition", "attachment; filename=exportedData.zip");// 设置响应头，指示浏览器下载文件
//            return new ResponseEntity<>(bytes, headers, HttpStatus.OK);// 返回响应实体
//        }
//    }



    @GetMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        try (FileInputStream fis = poolexzipTwoService.exportToZip()) {// 调用服务方法生成ZIP文件
            byte[] bytes = new byte[fis.available()];// 创建字节数组
            fis.read(bytes);// 读取ZIP文件内容到字节数组

            HttpHeaders headers = new HttpHeaders();// 创建HTTP响应头
            headers.add("Content-Disposition", "attachment; filename=exportedData.zip");// 设置响应头，指示浏览器下载文件
            return new ResponseEntity<>(bytes, headers, HttpStatus.OK);// 返回响应实体
        }
    }


    @GetMapping
    public String hello() {
        System.out.println("010分支");
        return "hello";
    }
}
